package com.example.suze.libraryinformationmanagementsystem.net;

import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Luo on 2017/6/17.
 * T 为 JavaBean类型(.entity中的类)
 */

public abstract class JSONCallBack<T> implements Callback {
    private T bean;

    public JSONCallBack(T typeEntity){
        bean = typeEntity;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        ResponseBody body = response.body();
//        try {
//            bean = (T)bean.getClass().newInstance();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
        Gson gson = new Gson();
        String bodyString = new String("".getBytes(),"UTF-8");
        bodyString = bodyString+body.string();
        Log.i("----->>>>>数据",bodyString);
        bean = (T) gson.fromJson(bodyString, bean.getClass());
        onSuccess(bean);
    }
    public abstract void onSuccess(T entity);
}
